// 顶部导航栏组件的 JS 代码
const app = getApp()
import Toast from 'tdesign-miniprogram/toast/index';
const CryptoJS = require('../../utils/crypto.js')
const WXAPI = require('../../wxapi/main')
const defaultAvatarUrl="https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0"

Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  properties: {
    authVisible: {
      type: Boolean,
      value: false
    }
  },
  data: {
    nickName:"",
    avatarUrl:"",
    randomAvatorMap:{
      1:"https://7072-prod-8g7u9tmqac56ab70-1318102458.tcb.qcloud.la/%E6%98%9F%E4%B9%8B%E5%8D%A1%E6%AF%94%E5%A4%B4%E5%83%8F.jpg?sign=f95187f6eeed59eb48f23fdb805974e9&t=1686229897",
      2:"https://7072-prod-8g7u9tmqac56ab70-1318102458.tcb.qcloud.la/%E7%9A%AE%E5%8D%A1%E4%B8%98%E5%A4%B4%E5%83%8F.jpg?sign=7c941f03a3d569ebec995551f8322171&t=1686229918",
      3:"https://7072-prod-8g7u9tmqac56ab70-1318102458.tcb.qcloud.la/%E9%A9%AC%E9%87%8C%E5%A5%A5%E5%A4%B4%E5%83%8F.jpg?sign=3a1181ef5e8882ced0ad7d7154dd5222&t=1686229933",
      4:"https://7072-prod-8g7u9tmqac56ab70-1318102458.tcb.qcloud.la/%E9%BE%99%E7%8F%A0%E5%A4%B4%E5%83%8F.jpg?sign=a02a29d8545112b48ccebc0f3bb72225&t=1686229947"
   }
  },
  lifetimes: {
  },
  methods: {
    //选择图片
  onChooseAvatar(e) {
    let that = this
    wx.getFileSystemManager().readFile({
      filePath: e.detail.avatarUrl, //地址
      encoding: 'base64', //编码格式
      success: res => {
        let base64 = 'data:image/png;base64,' + res.data
        that.setData({
          avatarUrl : base64
        })
      }
    })
  },
  queryUser() {

    

    WXAPI.queryUser().then(res=> {
      var userInfo = res;
      if(userInfo==undefined || userInfo==''){
        this.setData({
          authVisible:true,
          userInfo:{
            nick_name: "微信用户",
            avatar_url: defaultAvatarUrl
          }
           
         });
          return;
      }
      this.setData({
        userInfo: userInfo,
      });
      wx.setStorageSync("userInfo",userInfo);
      this.triggerEvent('callback')        

      wx.hideNavigationBarLoading();
    }).catch((e) => {
      wx.hideNavigationBarLoading();
    });

  },

    randomAvator(){
      var len=4
      var index=Math.floor(Math.random()*len)+1
      this.setData({
        avatarUrl:this.data.randomAvatorMap[index]
      })  
    },
    closeAuth(){
      this.setData({
        authVisible:false
      })
    },
    //获取昵称
    getName(e) {
      let nick_name = e.detail.value
      this.setData({
        nickName: nick_name
      })
    },
    formSubmit(){
      let nickName = this.data.nickName.trim()
        let avatarUrl = this.data.avatarUrl
        if(avatarUrl == defaultAvatarUrl){
          wx.showToast({
            title: '请选择头像',
            icon: 'none'
          })
          return;
        }else if(nickName.length==''){
          wx.showToast({
            title: '昵称不能为空',
            icon: 'none'
          })
          return;
        }else if((/[^/a-zA-Z0-9\u4E00-\u9FA5]/g).test(nickName)){
          wx.showToast({
            title: '昵称不合法',
            icon: 'none'
          })
          return;
        }
  
        WXAPI.addUser({
          nick_name:nickName,
          avatar_url:avatarUrl
        }).then(res=>{
            this.setData({
              authVisible:false
            })
            this.queryUser();
          })
    },
  }
});
